﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

using Elegant.Ui;

namespace iPdf
{
    class PrintingPermission
    {
        public PrintingPermissionId Id;
        string Description;

        static PrintingPermissionId CurrentId = 0;

        public PrintingPermission(string description)
        {
            Id = CurrentId;
            CurrentId++;

            Description = description;
        }

        public override string ToString()
        {
            return Description;
        }
    }

    class EditingPermission
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public EditingPermissionId Id;
        string Description;

        static EditingPermissionId CurrentId = 0;

        public EditingPermission(string description)
        {
            Id = CurrentId;
            CurrentId++;

            Description = description;
        }

        public override string ToString()
        {
            return Description;
        }
    }

    class SecurityPage<OptionType> : CertificatePage<OptionType>
    {
        #region Fields
        protected static SecurityLevel PasswordSecurity = new SecurityLevel(SecurityLevelId.PasswordSecurity, "Password Security");
        protected static SecurityLevel CertificateSecurity = new SecurityLevel(SecurityLevelId.CertificateSecurity, "Certificate Security");

        protected static SecurityLevel[] securityLevels = new SecurityLevel[]
        {
            PasswordSecurity,
            CertificateSecurity
        };
        
        protected static PrintingPermission[] printingPermissions = new PrintingPermission[] 
        {
            new PrintingPermission("None"), 
            new PrintingPermission("Low resolution"),
            new PrintingPermission("High resolution")
        };

        protected static EditingPermission[] editingPermissions = new EditingPermission[]
        {
            new EditingPermission("None"),
            new EditingPermission("Creating, inserting, deleting and rotating pages"),
            new EditingPermission("Filling-in form fields and signing"),
            new EditingPermission("Commenting, filling-in form fields and signing"),
            new EditingPermission("All except page extraction")
        };
        #endregion

        #region Properties
        public override bool ReadyToAct
        {
            get
            {
                bool ready = (OwnerPassword.Enabled) || 
                    (Certificate.Enabled && Certificate.IsValid &&
                    CertificatePassword.Enabled && !string.IsNullOrEmpty(CertificatePassword.Text) );
                return base.ReadyToAct && ready;
            }
        }

        protected Permission Permission
        {
            get
            {
                Permission permission = new Permission();
                permission.AllowCopyContent = CopyContentPermission.Checked;
                permission.AllowScreenReader = ScreenReaderPermission.Checked;
                permission.PrintingPermission = ((PrintingPermission)this.PrintingPermission.SelectedItem).Id;
                permission.EditingPermission = ((EditingPermission)this.EditingPermission.SelectedItem).Id;

                return permission;
            }
        }

        public override string ActionName
        {
            get
            {
                return base.ActionName + "By" +
                            (EncryptMethod.SelectedItem == PasswordSecurity ?
                        "Password" : "Certificate");
            }
        }

        #endregion

        protected void InitializeComboBoxes()
        {
            this.EncryptMethod.Items.AddRange(securityLevels);
            this.EncryptMethod.SelectedIndex = 0;
            this.EncryptMethod.SelectedIndexChanged += new EventHandler(EncryptMethod_SelectedIndexChanged);

            this.PrintingPermission.Items.AddRange(printingPermissions);
            this.PrintingPermission.SelectedIndex = 0;
            this.EditingPermission.Items.AddRange(editingPermissions);
            this.EditingPermission.SelectedIndex = 0;
        }


        protected void EncryptMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdatePageOnlyControls();
            Global.MainWindow.UpdateActionGroup();
        }

        public override void UpdatePageOnlyControls()
        {
            if (!componentsInitialized)
            {
                return;
            }
            if (Global.MainWindow.Busy || !Global.Grid.HasItems)
            {
                CryptoGroup.Enabled = false;
                PermissionsGroup.Enabled = false;
                return;
            }

            Helper.EnableControl(CryptoGroup, true);
            SecurityLevel level = (SecurityLevel)EncryptMethod.SelectedItem;
            bool isPasswordSecurity = level.Id == SecurityLevelId.PasswordSecurity;
            OwnerPassword.Enabled = isPasswordSecurity;
            Certificate.Enabled = !isPasswordSecurity;
            CertificatePassword.Enabled = !isPasswordSecurity &&
                Certificate.IsValid && Certificate.IsFile;

            ScreenReaderPermission.Enabled = !CopyContentPermission.Checked;
        }

        public override void OnTabPageChanged()
        {
            base.OnTabPageChanged();

            LoadCertificateSubgroup();
        }

        protected override void LoadCertificateSubgroup()
        {
            base.LoadCertificateSubgroup();
            this.CryptoGroup.Controls.Add(Certificate);
            this.CryptoGroup.Controls.Add(CertificatePassword);
        }

        private void EnablePermissions(bool enable)
        {
            this.CopyContentPermission.Checked = enable;
            this.ScreenReaderPermission.Checked = enable;

            if (enable == true)
            {
                this.PrintingPermission.SelectedIndex = this.PrintingPermission.Items.Count - 1;
                this.EditingPermission.SelectedIndex = this.EditingPermission.Items.Count - 1;
            }
            else
            {
                this.PrintingPermission.SelectedIndex = 0;
                this.EditingPermission.SelectedIndex = 0;
            }
        }

        protected void EnableAllAccess_Click(object sender, EventArgs e)
        {
            EnablePermissions(true);
        }

        protected void DenyAnyAccess_Click(object sender, EventArgs e)
        {
            EnablePermissions(false);
        }

        protected void CopyContentPermission_CheckedChanged(object sender, EventArgs e)
        {
            Elegant.Ui.CheckBox copyContentCheckBox = (Elegant.Ui.CheckBox)sender;
            ScreenReaderPermission.Enabled = !copyContentCheckBox.Checked;
            ScreenReaderPermission.Checked = copyContentCheckBox.Checked?true : ScreenReaderPermission.Checked;
        }

        public override void ImportSession(XmlElement sessionNode)
        {
            SetComboBoxBySession(sessionNode, EncryptMethod);

            OwnerPassword.Text = sessionNode.GetAttribute(OwnerPassword.Name);

            string certificate = sessionNode.GetAttribute(Certificate.Name);

            if (!string.IsNullOrEmpty(certificate))
            {
                Certificate.DefaultCertificate = new CertificateDropDownItem(Certificate, certificate,
                                    Certificate.CertificateItem_Click);
            }

            string certPassword = sessionNode.GetAttribute(CertificatePassword.Name);
            CertificatePassword.Text = certPassword;

            SetCheckBoxBySession(sessionNode, ScreenReaderPermission);
            SetCheckBoxBySession(sessionNode, CopyContentPermission);
            
            SetComboBoxBySession(sessionNode, PrintingPermission);
            SetComboBoxBySession(sessionNode, EditingPermission);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
        public override void ExportSession(XmlElement sessionNode)
        {
            SetXmlElementAttribute(sessionNode, EncryptMethod.Name, 
                            (EncryptMethod.SelectedIndex + 1).ToString() );
            SetXmlElementAttribute(sessionNode, OwnerPassword.Name, OwnerPassword.Text);

            if (Certificate.DefaultCertificate != null &&
                 !string.IsNullOrEmpty(Certificate.DefaultCertificate.Text))
            {
                sessionNode.SetAttribute(Certificate.Name, Certificate.DefaultCertificate.Text);
            }
            SetXmlElementAttribute(sessionNode, CertificatePassword.Name, CertificatePassword.Text);

            //Permissions
            SetXmlElementAttribute(sessionNode, CopyContentPermission.Name, 
                                CopyContentPermission.Checked.ToString());
            SetXmlElementAttribute(sessionNode, ScreenReaderPermission.Name, 
                                ScreenReaderPermission.Checked.ToString());
            SetXmlElementAttribute(sessionNode, PrintingPermission.Name, 
                                (PrintingPermission.SelectedIndex + 1).ToString() );
            SetXmlElementAttribute(sessionNode, EditingPermission.Name,
                                (EditingPermission.SelectedIndex + 1).ToString() );
        }

        protected override void InitializeComponent()
        {
            base.InitializeComponent();
            //Crypto group
            this.CryptoGroup = new Elegant.Ui.RibbonGroup();
            this.EncryptMethod = new Elegant.Ui.ComboBox();
            this.OwnerPassword = new iPdf.HoverSelectionTextBox();

            //Permission group
            this.PermissionsGroup = new Elegant.Ui.RibbonGroup();
            this.CopyContentPermission = new Elegant.Ui.CheckBox();
            this.ScreenReaderPermission = new Elegant.Ui.CheckBox();
            this.PrintingPermission = new Elegant.Ui.ComboBox();
            this.EditingPermission = new Elegant.Ui.ComboBox();
            this.DenyAnyAccess = new Elegant.Ui.Button();
            this.EnableFullAccess = new Elegant.Ui.Button();

            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.CryptoGroup)).BeginInit();
            this.CryptoGroup.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.PermissionsGroup)).BeginInit();
            this.PermissionsGroup.SuspendLayout();


            this.Controls.Add(this.CryptoGroup);

            // 
            // CryptoGroup
            // 
            this.CryptoGroup.Controls.Add(this.EncryptMethod);
            this.CryptoGroup.Controls.Add(this.OwnerPassword);
            this.CryptoGroup.DialogLauncherButtonVisible = false;
            this.CryptoGroup.Id = Helper.GenerateGuid();
            this.CryptoGroup.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Crypto)});
            this.CryptoGroup.Location = new System.Drawing.Point(4, 3);
            this.CryptoGroup.Name = "CryptoGroup";
            this.CryptoGroup.Size = new System.Drawing.Size(434, 94);
            this.CryptoGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Crypto16)});
            this.CryptoGroup.TabIndex = 5;
            this.CryptoGroup.Text = "Cryptography";
            // 
            // EncryptMethod
            // 
            this.EncryptMethod.DrawMode = System.Windows.Forms.DrawMode.Normal;
            this.EncryptMethod.DroppedDown = false;
            this.EncryptMethod.Editable = false;
            this.EncryptMethod.FormatInfo = null;
            this.EncryptMethod.FormatString = "";
            this.EncryptMethod.FormattingEnabled = false;
            this.EncryptMethod.Id = Helper.GenerateGuid();
            this.EncryptMethod.InformativenessMinimumLevel = "Elegant.Ui.ComboBoxInformativenessLevel:ComboBoxWithLabel";
            this.EncryptMethod.LabelAreaWidthTemplate = "Owner Password";
            this.EncryptMethod.LabelText = "Security Method";
            this.EncryptMethod.Location = new System.Drawing.Point(4, 2);
            this.EncryptMethod.Name = "EncryptMethod";
            this.EncryptMethod.Size = new System.Drawing.Size(212, 24);
            this.EncryptMethod.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Security16)});
            this.EncryptMethod.Sorted = false;
            this.EncryptMethod.TabIndex = 0;
            this.EncryptMethod.TextEditorWidth = 82;

            // 
            // OwnerPassword
            // 
            this.OwnerPassword.Id = Helper.GenerateGuid();
            this.OwnerPassword.ImeMode = System.Windows.Forms.ImeMode.Disable;
            this.OwnerPassword.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.OwnerPassword.LabelAreaWidthTemplate = "";
            this.OwnerPassword.LabelText = "Owner Password";
            this.OwnerPassword.Location = new System.Drawing.Point(4, 26);
            this.OwnerPassword.Name = "OwnerPassword";
            this.OwnerPassword.PasswordChar = '●';
            this.OwnerPassword.Size = new System.Drawing.Size(211, 24);
            this.OwnerPassword.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Owner16)});
            this.OwnerPassword.TabIndex = 1;
            this.OwnerPassword.TextChanged += new EventHandler(TextBox_TextChanged);
            this.OwnerPassword.TextEditorWidth = 95;
            this.OwnerPassword.UseSystemPasswordChar = true;

            this.Controls.Add(this.PermissionsGroup);
            // 
            // PermissionGroup
            // 
            this.PermissionsGroup.Controls.Add(this.CopyContentPermission);
            this.PermissionsGroup.Controls.Add(this.ScreenReaderPermission);
            this.PermissionsGroup.Controls.Add(this.PrintingPermission);
            this.PermissionsGroup.Controls.Add(this.EditingPermission);
            this.PermissionsGroup.Controls.Add(this.DenyAnyAccess);
            this.PermissionsGroup.Controls.Add(this.EnableFullAccess);
            this.PermissionsGroup.DialogLauncherButtonVisible = false;
            this.PermissionsGroup.Id = "3dc5276d-63f0-44b4-8d1b-e19bd53e96e8";
            this.PermissionsGroup.Location = new System.Drawing.Point(240, 3);
            this.PermissionsGroup.Name = "PermissionGroup";
            this.PermissionsGroup.Size = new System.Drawing.Size(323, 94);
            this.PermissionsGroup.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Permission)});
            this.PermissionsGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Permission16)});
            this.PermissionsGroup.TabIndex = 1;
            this.PermissionsGroup.Text = "Permissions";
            // 
            // CopyContentPermission
            // 
            this.CopyContentPermission.Id = "70377df1-f7ce-4294-9ce6-aa5b614b5f3b";
            this.CopyContentPermission.Location = new System.Drawing.Point(4, 26);
            this.CopyContentPermission.Name = "CopyContentPermission";
            this.CopyContentPermission.Size = new System.Drawing.Size(138, 24);
            this.CopyContentPermission.TabIndex = 0;
            this.CopyContentPermission.Text = "Allow Copying of Content";
            this.CopyContentPermission.CheckedChanged +=
                new EventHandler(CopyContentPermission_CheckedChanged);
            // 
            // ScreenReaderPermission
            // 
            this.ScreenReaderPermission.Id = "ef3ccd3a-de11-45ee-abb4-ea27c457182a";
            this.ScreenReaderPermission.Location = new System.Drawing.Point(4, 2);
            this.ScreenReaderPermission.Name = "ScreenReaderPermission";
            this.ScreenReaderPermission.Size = new System.Drawing.Size(120, 24);
            this.ScreenReaderPermission.TabIndex = 3;
            this.ScreenReaderPermission.Text = "Allow Screen Reader";
            // 
            // PrintingPermission
            // 
            this.PrintingPermission.BackColor = System.Drawing.SystemColors.Window;
            this.PrintingPermission.DrawMode = System.Windows.Forms.DrawMode.Normal;
            this.PrintingPermission.DroppedDown = false;
            this.PrintingPermission.Editable = false;
            this.PrintingPermission.FormatInfo = null;
            this.PrintingPermission.FormatString = "";
            this.PrintingPermission.FormattingEnabled = true;
            this.PrintingPermission.Id = "6d8daa9d-7680-4a62-96eb-502f82f69116";
            this.PrintingPermission.LabelAreaWidthTemplate = "";
            this.PrintingPermission.LabelText = "Print";
            this.PrintingPermission.Location = new System.Drawing.Point(4, 50);
            this.PrintingPermission.InformativenessMinimumLevel = "Elegant.Ui.ComboBoxInformativenessLevel:ComboBoxWithImageAndLabel";
            this.PrintingPermission.Name = "PrintingPermission";
            this.PrintingPermission.Size = new System.Drawing.Size(154, 24);
            this.PrintingPermission.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Print16)});
            this.PrintingPermission.Sorted = false;
            this.PrintingPermission.TabIndex = 4;
            this.PrintingPermission.Text = "Not Allowed";
            this.PrintingPermission.TextEditorWidth = 92;
            // 
            // EditingPermission
            // 
            this.EditingPermission.BackColor = System.Drawing.SystemColors.Window;
            this.EditingPermission.DrawMode = System.Windows.Forms.DrawMode.Normal;
            this.EditingPermission.DroppedDown = false;
            this.EditingPermission.Editable = false;
            this.EditingPermission.FormatInfo = null;
            this.EditingPermission.FormatString = "";
            this.EditingPermission.FormattingEnabled = true;
            this.EditingPermission.Id = "2a4ee48c-62ec-419e-9a87-6a350d7454d9";
            this.EditingPermission.LabelAreaWidthTemplate = "";
            this.EditingPermission.LabelText = "Edit";
            this.EditingPermission.Location = new System.Drawing.Point(160, 2);
            this.EditingPermission.InformativenessMinimumLevel = "Elegant.Ui.ComboBoxInformativenessLevel:ComboBoxWithImageAndLabel";
            this.EditingPermission.Name = "EditingPermission";
            this.EditingPermission.Size = new System.Drawing.Size(158, 24);
            this.EditingPermission.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Edit16)});
            this.EditingPermission.Sorted = false;
            this.EditingPermission.TabIndex = 5;
            this.EditingPermission.Text = "Not allowed";
            this.EditingPermission.TextEditorWidth = 99;
            // 
            // DenyAnyAccess
            // 
            this.DenyAnyAccess.Id = "7125b609-700d-448c-9299-82166da91142";
            this.DenyAnyAccess.Click += new EventHandler(DenyAnyAccess_Click);
            this.DenyAnyAccess.InformativenessMaximumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:SmallImageWithText";
            this.DenyAnyAccess.InformativenessMinimumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:SmallImageWithText";
            this.DenyAnyAccess.Location = new System.Drawing.Point(145, 2);
            this.DenyAnyAccess.Name = "DenyAnyAccess";
            this.DenyAnyAccess.Size = new System.Drawing.Size(146, 24);
            this.DenyAnyAccess.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.DisableAllPermissions16)});
            this.DenyAnyAccess.TabIndex = 4;
            this.DenyAnyAccess.Text = "Deny Any Access";
            // 
            // EnableFullAccess
            // 
            this.EnableFullAccess.Id = "e516fa47-8f94-45c0-b906-d9854d060e8f";
            this.EnableFullAccess.Click += new EventHandler(EnableAllAccess_Click);
            this.EnableFullAccess.InformativenessMaximumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:SmallImageWithText";
            this.EnableFullAccess.InformativenessMinimumLevel = "Elegant.Ui.RibbonGroupButtonInformativenessLevel:SmallImageWithText";
            this.EnableFullAccess.Location = new System.Drawing.Point(145, 26);
            this.EnableFullAccess.Name = "EnableAllPermissions";
            this.EnableFullAccess.Size = new System.Drawing.Size(143, 24);
            this.EnableFullAccess.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.EnableAllPermissions16)});
            this.EnableFullAccess.TabIndex = 5;
            this.EnableFullAccess.Text = "Enable Full Access";


            ((System.ComponentModel.ISupportInitialize)(this.CryptoGroup)).EndInit();
            this.CryptoGroup.ResumeLayout(false);
            this.CryptoGroup.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.PermissionsGroup)).EndInit();
            this.PermissionsGroup.ResumeLayout(false);
            this.PermissionsGroup.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

            InitializeComboBoxes();
        }

        #region Fields
        //Cryptography group
        protected Elegant.Ui.RibbonGroup CryptoGroup;
        protected Elegant.Ui.ComboBox EncryptMethod;
        protected Elegant.Ui.TextBox OwnerPassword;
        
        //Permission group
        protected Elegant.Ui.RibbonGroup PermissionsGroup;
        protected Elegant.Ui.CheckBox CopyContentPermission;
        protected Elegant.Ui.CheckBox ScreenReaderPermission;
        protected Elegant.Ui.ComboBox EditingPermission;
        protected Elegant.Ui.ComboBox PrintingPermission;
        protected Elegant.Ui.Button DenyAnyAccess;
        protected Elegant.Ui.Button EnableFullAccess;
        #endregion
    }
}
